package org.aeon.service.impl;

import org.aeon.dao.FriendApplyDao;
import org.aeon.entity.FriendApply;
import org.aeon.entity.dto.FriendApplyDto;
import org.aeon.service.FriendApplyService;
import org.aeon.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author: MissingAeon
 * @date: 2024/2/24 15:03
 * @description:
 */
public class FriendApplyServiceImpl implements FriendApplyService {
    @Override
    public boolean isApplyExist(Long userId, Long userId2) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession(true)) {
            final FriendApplyDao mapper = sqlSession.getMapper(FriendApplyDao.class);
            return mapper.getValidByUserId(userId, userId2) != null;
        }
    }

    @Override
    public void addFriendApply(FriendApply friendApply) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession(true)) {
            final FriendApplyDao mapper = sqlSession.getMapper(FriendApplyDao.class);
            mapper.insert(friendApply);
        }
    }

    @Override
    public List<FriendApplyDto> stackingMessages(Long userId) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession(true)) {
            final FriendApplyDao mapper = sqlSession.getMapper(FriendApplyDao.class);
            return mapper.getByAnotherUserId(userId);
        }
    }

    @Override
    public int deleteFriendApply(Long id) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession(true)) {
            final FriendApplyDao mapper = sqlSession.getMapper(FriendApplyDao.class);
            return mapper.delById(id);
        }
    }

    @Override
    public int updateFriendApplyResult(FriendApply friendApply) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession(true)) {
            final FriendApplyDao mapper = sqlSession.getMapper(FriendApplyDao.class);
            return mapper.updateResult(friendApply);
        }
    }

    @Override
    public List<FriendApplyDto> getMessages(Long userId) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession(true)) {
            final FriendApplyDao mapper = sqlSession.getMapper(FriendApplyDao.class);
            return mapper.getAllByAnotherUserId(userId);
        }
    }
}
